<template>
  <div class="root">
    <div class="baseinf">
      <div class="name">{{parmas.name}}</div>
      <div class="address">{{parmas.adress}}</div>
      <div class="phone">联系电话：<span class="span1">{{parmas.phone}}</span></div>
      <div class="phone">所属行业：<span class="span1">{{parmas.industry}}</span></div>
      <div class="phone">覆盖范围：<span class="span1">{{parmas.distance}}</span></div>
      <div class="phone">关  键  词：<span class="span1">{{parmas.keyword}}</span></div>
    </div>
    <div class="Package">
      <div class="title">商品信息</div>
      <div class="producitem" v-for="(item,index) in produc" :key="item.id" @click="clickproduc(item)">
        <img class="icon":src="item.icon">
        <div class="name">{{item.name}}</div>
        <div class="money">{{'￥' + (item.money/100).toFixed(2)}}</div>
        <img class="Check" v-if="modify != item.id" v-show="defulproduc.id != item.id" src="../../assets/weixuan.png">
        <img class="active" v-if="modify != item.id" v-show="defulproduc.id == item.id" src="../../assets/xunzhong.png">
      </div>
    </div>
    <div class="payment">
        <div class="title">支付方式</div>
        <div class="paymentitem" v-for="(item,index) in payuse" :key="index" @click="clickpayuse(item)">
            <img class="image" :src="item.url">
            <div class="name">{{item.name}}</div>
            <img class="active" :src="defulpay.id === item.id?require('../../assets/xunzhong.png') : require('../../assets/weixuan.png')">
        </div>
    </div>
    <div class="clickpay" @click="topay">确认入驻</div>

  </div>
</template>

<script>
import { getproduc, getpayuse, payments } from '@/api/crateoder'
import { getchannelno, gethref } from '@/utils/storage'
import request from '@/utils/request'
import { mapState } from 'vuex'
export default  {
  name: 'CratteOderIndex',
  data () {
    return {
      parmas: {},
      produc:[], // 产品的列表
      modify: 0, // 默认第一个产品，不会被选择的
      defulproduc: {}, // 选中的产品
      payuse: [],   // 支付方式
      defulpay: {} // 选中的支付方式
    }
  },
  // 页面创建之前
  created () {
    console.log('111',this.$route.query)
    this.parmas = this.$route.query
    // 获取产品
      this.getproduc()
    // 获取支付方式
      this.getpayuse()
  },
  // 计算属性
  computed: {
    ...mapState('user',['submitdata']),
  },
  methods: {
    request,
    // 获取产品然后操作
    async getproduc () {
      const {data} = await getproduc()
      console.log("获取产品",data)
      // 第一个不到选中框
      this.modify = data[0].id
      this.defulproduc = data[1]
      this.produc = data
    },
    // 点击选择产品
    clickproduc (item) {
      this.defulproduc = item
      console.log('我选择的产品',this.defulproduc)
    },
    // 获取支付方式
    async getpayuse () {
      const { data } = await getpayuse()
      data.forEach((item,index)=>{
        const obj = {}
        if(item === "ZFB_H5"){
            obj.id = "ZFB_H5"
            obj.name = "支付宝"
          // v-bind绑定必须带上require
            obj.url = require("../../assets/zhifubao.png")
        }else if(item === "WX_H5"){
          obj.id = "WX_H5"
          obj.name = "微信"
          obj.url = require("../../assets/weixing.png")
        }
        this.payuse.push(obj)
      })
      this.defulpay = this.payuse[0]
      console.log("获取支付方式",this.payuse)
    },
    // 点击切换支付
    clickpayuse (item) {
      this.defulpay = item
      console.log('选择的支付方式',this.defulpay)
    },
    // 点击支付
    async topay () {
      // 处理一下穿过去的首页链接
      const url = gethref()
      const htmlIndex = url.indexOf('.html');
      // 如果找到了 .html，则截取之前的部分，否则保持原样
      const newUrl = htmlIndex !== -1 ? url.slice(0, htmlIndex + 5) : url;
      const processedUrl = newUrl + '#/payresult'
      let params = {
        productId: this.defulproduc.id,
        returnUrl: processedUrl,
        payType: this.defulpay.id,
        phone: this.parmas.phone,
        name: this.parmas.name,
        merchantId: this.$store.state.user.submitdata.id,
        channelNo: getchannelno(),
        link: gethref(),
      }
      // console.log('params', this.$store.state.user.submitdata.id)
       const result = await payments(params)
       window.location.href = result.data.payUrl
      // console.log('111',result.payUrl)
    }
  }
}
</script>

<style lang="less" scoped>
.root {
  width: 100vw;
  height: 100vh;
  margin: 0 auto;
  font-family: '思源黑体';
  background: linear-gradient(180deg, rgba(35, 98, 238, 1) 0%, rgba(215, 223, 247, 1) 34.72%);
  max-width: 375px;
  overflow: auto;
  .baseinf{
    width: 360px;
    //height: 200px;
    padding: 8px 0px;
    background-color: rgba(255, 255, 255, 1);
    margin: 0 auto;
    margin-top: 8px;
    border-radius: 6px;
    .name{
      width: 330px;
      height: 40px;
      margin: 0 auto;
      font-size: 18px;
      font-weight: 700;
      line-height: 40px;
      color: rgba(0, 0, 0, 1);
    }
    .address{
      width: 330px;
      margin: 0 auto;
      font-size: 14px;
      font-weight: 400;
      margin-top: 6px;
      margin-bottom: 12px;
      line-height: 20px;
      color: rgba(56, 56, 56, 1);
    }
    .phone{
      width: 330px;
      height: 22px;
      margin: 0 auto;
      margin-top: 8px;
      margin-bottom: 4px;
      //background-color: pink;
      font-size: 14px;
      font-weight: 400;
      line-height: 22px;
      color: rgba(128, 128, 128, 1);
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      .span1{
        font-size: 14px;
        font-weight: 400;
        color: rgba(0, 0, 0, 1);
      }
    }
  }
  .Package{
    width: 360px;
    padding: 8px 0px;
    background-color: rgba(255, 255, 255, 1);
    margin: 0 auto;
    margin-top: 8px;
    border-radius: 6px;
    .title{
      width: 350px;
      height: 30px;
      margin: 0 auto;
      font-size: 16px;
      font-weight: 700;
      line-height: 30px;
      color: rgba(0, 0, 0, 1);
    }
    //遍历产品
    .producitem{
      width: 350px;
      height: 40px;
      //background-color: pink;
      margin: 0 auto;
      margin-top: 10px;
      margin-bottom: 10px;
      display: flex;
      .icon{
        width: 24px;
        height: 24px;
        margin: 8px;
      }
      .name{
        width: 200px;
        height: 40px;
        font-size: 14px;
        font-weight: 500;
        line-height: 40px;
        color: rgba(0, 0, 0, 1);
      }
      .money{
        width: 70px;
        height: 40px;
        font-size: 14px;
        font-weight: 400;
        //background-color: #4d97ff;
        line-height: 40px;
        text-align: right;
        color: rgba(0, 0, 0, 1);
      }
      .Check{
        width: 22px;
        height: 22px;
        margin: 10px;
      }
      .active{
        width: 22px;
        height: 22px;
        margin: 10px;
      }
    }
  }
  .payment{
    width: 360px;
    padding: 8px 0px;
    background-color: rgba(255, 255, 255, 1);
    margin: 0 auto;
    margin-top: 8px;
    border-radius: 6px;
    .title{
      width: 340px;
      height: 40px;
      margin: 0 auto;
      font-size: 20px;
      font-weight: 700;
      line-height: 40px;
      color: rgba(0, 0, 0, 1);
    }
    .paymentitem{
      width: 340px;
      height: 40px;
      margin: 0 auto;
      margin-top: 10px;
      margin-bottom: 10px;
      //background-color: pink;
      display: flex;
      .image{
        width: 30px;
        height: 30px;
        margin: 5px;
      }
      .name{
        width: 100px;
        height: 40px;
        font-size: 16px;
        line-height: 40px;
        color: rgba(0, 0, 0, 1);
        margin-left: 10px;
      }
      .active{
        width: 26px;
        height: 26px;
        margin: 7px;
        margin-left: 140px;
      }
    }
  }
  .clickpay{
    width: 320px;
    height: 54px;
    margin: 0 auto;
    margin-top: 20px;
    border-radius: 30px;
    background: linear-gradient(154.49deg, rgba(77, 151, 255, 1) 0%, rgba(32, 122, 247, 1) 100%);
    font-size: 22px;
    font-weight: 700;
    line-height: 54px;
    color: rgba(255, 255, 255, 1);
    text-align: center;
  }
}
</style>
